package my.soft.aiteacher.teacher.mapStruct;

import com.alibaba.fastjson.JSONObject;
import my.soft.aiteacher.teacher.pojo.QuestionDO;
import my.soft.aiteacher.teacher.pojo.QuestionVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.Map;
import java.util.stream.Collectors;

@Mapper(componentModel = "spring")
public interface QuestionMapStruct {
    @Mapping(target = "options", source = "options", qualifiedByName = "jsonToMap")
    QuestionVO questionDOToQuestionVO(QuestionDO questionDO);

    @Mapping(target = "options", source = "options", qualifiedByName = "mapToJson")
    QuestionDO questionVOToQuestionDO(QuestionVO questionVO);

    @Named("jsonToMap")
    default Map<String, String> jsonToMap(JSONObject json) {
        return json == null ? null : json.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        e -> e.getValue().toString()
                ));
    }

    @Named("mapToJson")
    default JSONObject mapToJson(Map<String, String> map) {
        if (map == null) {
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.putAll(map);
        return jsonObject;
    }
}
